package team.bluepen.supermarket.data.dao;

import org.apache.hadoop.hbase.util.Bytes;
import team.bluepen.supermarket.constant.Constant;
import team.bluepen.supermarket.constant.Error;
import team.bluepen.supermarket.data.DatabaseHelper;
import team.bluepen.supermarket.data.dao.abstraction.Repository;
import team.bluepen.supermarket.data.entity.Good;

import java.io.IOException;
import java.util.List;

/**
 * {@link Good} 存储库
 * @author Kai
 */
public class GoodRepository extends Repository<Good> {
    private static final String TAG = "GoodRepository";

    public GoodRepository() {
        super(DatabaseHelper.getGoodDatabase());
    }


    public Error createGood(Good good) {
        try {
            List<Good> res = database.query(Constant.GoodTable.TABLE_FAMILY_1,
                    Constant.GoodTable.QUALIFIER_ID, Bytes.toBytes(good.getId()));
            if (res != null && !res.isEmpty()) {
                return Error.ERROR_DATA_EXISTED;
            } else {
                good.generateRowKey();
                return database.insert(good);
            }
        } catch (IOException e) {
            return Error.ERROR_DATABASE;
        }
    }
}
